<?php
class TermController extends Blog_Controller_AdminController {

	public function indexAction() {
		// action body
	}
	
	public function addAction() {
		$Type = ucfirst($this->getRequest()->getParam('type'));
		$Form = new Zend_Form(array('name' => 'AddTerm'));
		$Form->addElements(array(
			new Zend_Form_Element_Text('TermName', array('validators' => array(array('NotEmpty')))),
			new Zend_Form_Element_Submit('submit', 'Lưu')
		));
		
		if ($this->getRequest()->isPost()) {
			
			if ($Form->isValid($this->getRequest()->getPost())) {
				$Meta = require APPLICATION_PATH . '/configs/term.php';
				$Mapper = new Admin_Model_Term_Mapper('Db');
				$Term = new Admin_Model_Term_Term();
				$Term->setName($Form->getValue('TermName'))
						->setValue('')
						->setType(2)
						->setParentId($Meta[$Type]);
				$Mapper->save($Term);
			}
		}
		
		$this->view->assign('AddTerm', $Form);
	}
	
	public function listAction() {
		$Type = $this->getRequest()->getParam('type');
		$Meta = require APPLICATION_PATH . '/configs/term.php';
		//$Term = new Admin_Model_Term_Term();
		$Mapper = new Admin_Model_Term_Mapper('Db');
		$Paginator = $Mapper->fetchPagination(array(array('where' => 'Type = ?', 'value' => 2), array('where' => 'ParentID = ?', 'value' => $Meta[$Type])), 'Order ASC');
		$Paginator->setCurrentPageNumber((int) $this->getRequest()->getParam('page'))->setItemCountPerPage(20)->setPageRange(5);
		
		$this->view->assign('Type', strtolower($Type));
		$this->view->assign('Terms', $Paginator->getCurrentItems());
		$this->view->assign('Paginator', $Paginator->render());
	}

	public function __call($method, $args) {
		if ('list' == substr($method, 0, 4)) {
			$this->forward('list', null, null, array('type' => substr($method, 4, strlen($method) - 10)));
		} elseif ('add' == substr($method, 0, 3)) {
			$this->forward('add', null, null, array('type' => substr($method, 3, strlen($method) - 9)));
		}
	}
}